package com.wd.servlet;

import cn.hutool.core.date.DateTime;
import com.wd.entity.User;
import com.wd.service.UserService;
import com.wd.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/11/8 19:43
 * @Description: ${description}
 * @Version: 1.0
 */
@WebServlet(urlPatterns = "/EditUserServlet")
public class EditUserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
            HttpSession hs = request.getSession();

            boolean flag = true;
            String op = request.getParameter("op");
            String userId = request.getParameter("userId");
            System.out.println("userId:"+userId);
            System.out.println("EditUserServlet+OP："+op);

            if (op != null && !"null".equals(op) && !"".equals(op)) {
                flag = false;
                switch (op) {
                    //修改blog信息
                    case "updUserById":
                        updUserById(request, response, hs);
                        break;
                    default:
                        PrintWriter out = response.getWriter();
                        out.print("无效的指令");
                        out.close();
                }
            }
            String op1 = (String) hs.getAttribute("op1");
            /**
             */
            if (op1 != null && !"null".equals(op1) && !"".equals(op1)) {
                flag = false;
                hs.setAttribute("op1", "");
                switch (op1) {

                    //先根据id查询信息到页面展示
                    case "selUserById":
                        selUserById(request, response, hs);
                        op1 = "";
                        break;
                    default:
                        PrintWriter out = response.getWriter();
                        out.print("无效的指令");
                        out.close();
                }
            }
            if (flag) {
                request.getRequestDispatcher("user.jsp").forward(request, response);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void updUserById(HttpServletRequest request, HttpServletResponse response,HttpSession hs)
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userId = request.getParameter("userId");
        String accont = request.getParameter("account2");
        String password = request.getParameter("password");
        String problem = request.getParameter("problem2");
        String answer = request.getParameter("answer2");
        DateTime createTime = DateTime.of(request.getParameter("createTime"), "yyyy-MM-dd HH:mm:ss");
        String identify = request.getParameter("identify2");
        User user = new User(Integer.parseInt(userId),accont,password,problem,answer,createTime,identify);
        System.out.println(user);
        int i = userService.updUser(user);
        hs.setAttribute("blog", userService.selUserById(Integer.parseInt(userId)));
        if (i == 1) {
            hs.setAttribute("MSG", 200);
            response.sendRedirect("UserServlet");
        } else {
            hs.setAttribute("MSG", 201);
            response.sendRedirect("UserServlet");
        }

    }

    protected void selUserById(HttpServletRequest request, HttpServletResponse response,HttpSession hs)
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        int userId = Integer.parseInt((String)request.getSession().getAttribute("userId"));
        UserService userService = new UserServiceImpl();
        User user = userService.selUserById(userId);
        request.getSession().setAttribute("user",user);
        request.getRequestDispatcher("edituser.jsp").forward(request,response);
    }
}
